/**
 * @fileoverview
 *
 * UXperf configuration utility
 * TODO: implement preserving configuration between requests using localStorage.
 *
 * @author Sebastian Brzuzek sebastian@brzuzek.pl
 */

goog.provide('uxperf.Config');

goog.require('goog.object');
goog.require('goog.array');
goog.require('uxperf.utils');

/**
 * Configuration values
 * 
 * @type {!Object}
 * @private
 */
uxperf.Config.values_ = {
//	'config_preserve': null,//'preserve': 'localStorage', //TODO save configuration in local storage is reasonable if there is one entrypoint to application
//  'audio': null //reserved for future support of sound effects
};

/**
 * Set custom configuration values
 * 
 * @param {!Object} cfg configuration dataset
 * @public
 */
uxperf.Config.set = function (cfg) {
	uxperf.utils.overrides(uxperf.Config.values_, cfg);
};

/**
 * Set default configuration values
 * 
 * @param {!Object} def default configuration dataset
 * @public
 */
uxperf.Config.setDefaults = function (def) {
	uxperf.utils.defaults(uxperf.Config.values_, def);
};

/**
 * Get configuration value
 *
 * @param {...(string|number|!Array.<number|string>)} var_args A number of keys
 *     (as strings, or nubmers, for array-like objects) to retrieve specific value
 *     from configuration structure. Can also be specified as a single array of keys.
 * 
 * @return {*} The resulting value. If, at any point, the value for a key
 *     is undefined, returns undefined.
 * 
 * @public
 */
uxperf.Config.get = function (var_args) {
  //if (goog.DEBUG) LOG.info('Config - get - arguments: ' + goog.array.toArray(arguments));
	if (arguments.length>1) var_args = goog.array.toArray(arguments);
  var res = goog.object.getValueByKeys(uxperf.Config.values_, var_args);
  //if (goog.DEBUG) LOG.info('Config - get - result: ' + res);
	return res;
};

/**
 * Get subconfiguration value for given config name
 *
 * @param {string} name name of subconfiguration
 * @param {...(string|number|!Array.<number|string>)} var_args keys
 * 
 * @return {*} The resulting value from configuration.
 *              If, at any point, the value for a key
 *              is undefined, returns undefined.
 * @public
 */
uxperf.Config.getSub = function (name, var_args) {
  //if (goog.DEBUG) LOG.info('Config - getSub - arguments: ' + goog.array.toArray(arguments));
	if (arguments.length > 2) {
		return uxperf.Config.get(goog.array.toArray(arguments));
	}
	if ((typeof var_args === 'number') || (typeof var_args === 'string')) {
		return uxperf.Config.get(name, var_args);
	}
  //if (goog.DEBUG) LOG.info('Config - getSub - single array (with unshift)');
	var_args = goog.array.toArray(var_args || []);
	var_args.unshift(name);
  //if (goog.DEBUG) LOG.info('Config - getSub - var_args : ' + var_args);
	return uxperf.Config.get(var_args);
}
